package org.zwy.gradetrack.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.zwy.gradetrack.common.Result;
import org.zwy.gradetrack.entity.Course;
import org.zwy.gradetrack.entity.Major;
import org.zwy.gradetrack.service.CourseService;
import org.zwy.gradetrack.service.MajorService;

import java.util.List;

/**
 * 课程管理控制器
 */
@RestController
@RequestMapping("/api/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private MajorService majorService;

    /**
     * 获取课程列表
     */
    @GetMapping
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER','GRADUATE')")
    public Result<List<Course>> getCourseList(@RequestParam(required = false) Long majorId) {
        List<Course> courses;
        if (majorId != null) {
            // 检查专业是否存在
            if (!majorService.lambdaQuery()
                    .eq(Major::getId, majorId)
                    .exists()) {
                return Result.error(404, "专业不存在");
            }

            courses = courseService.lambdaQuery()
                    .eq(Course::getMajorId, majorId)
                    .list();
        } else {
            courses = courseService.list();
        }
        return Result.success(courses);
    }

    /**
     * 创建课程
     */
    @PostMapping
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public Result<Course> createCourse(@RequestBody Course course) {
        // 检查专业是否存在
        if (!majorService.lambdaQuery()
                .eq(Major::getId, course.getMajorId())
                .exists()) {
            return Result.error(404, "专业不存在");
        }

        // 检查课程代码是否已存在
        if (courseService.lambdaQuery()
                .eq(Course::getCourseCode, course.getCourseCode())
                .exists()) {
            return Result.error(400, "课程代码已存在");
        }

        // 保存课程信息
        courseService.save(course);
        return Result.success(course);
    }

    /**
     * 更新课程
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public Result<Course> updateCourse(@PathVariable Long id, @RequestBody Course course) {
        // 检查课程是否存在
        if (!courseService.lambdaQuery()
                .eq(Course::getId, id)
                .exists()) {
            return Result.error(404, "课程不存在");
        }

        // 检查专业是否存在
        if (!majorService.lambdaQuery()
                .eq(Major::getId, course.getMajorId())
                .exists()) {
            return Result.error(404, "专业不存在");
        }

        // 检查课程代码是否重复
        if (courseService.lambdaQuery()
                .eq(Course::getCourseCode, course.getCourseCode())
                .ne(Course::getId, id)
                .exists()) {
            return Result.error(400, "课程代码已存在");
        }

        // 更新课程信息
        course.setId(id);
        courseService.updateById(course);
        return Result.success(course);
    }

    /**
     * 删除课程
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public Result<Void> deleteCourse(@PathVariable Long id) {
        // 检查课程是否存在
        if (!courseService.lambdaQuery()
                .eq(Course::getId, id)
                .exists()) {
            return Result.error(404, "课程不存在");
        }

        // 删除课程
        courseService.removeById(id);
        return Result.success();
    }

    /**
     * 获取课程详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public Result<Course> getCourseDetail(@PathVariable Long id) {
        Course course = courseService.getById(id);
        if (course == null) {
            return Result.error(404, "课程不存在");
        }
        return Result.success(course);
    }
} 